package UI;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ModifyUserinfor_UI{
    private JFrame mainJFrame;
    private JDialog dialog=new JDialog(mainJFrame,"修改用户数据",JDialog.DEFAULT_MODALITY_TYPE);
    final int width=600;
    final int height=400;

    JLabel usernameJLabel=new JLabel("姓        名：");
    JTextField usernameJTe=new JTextField(20);
    JLabel tellJLabel=new JLabel("电        话：");
    JTextField tellJTe=new JTextField(20);
    JLabel addressJLabel=new JLabel("住        址：");
    JTextField addreddJTe=new JTextField(20);
    JLabel cardJLabel=new JLabel("卡    号：");
    JLabel cardJta=new JLabel();
    JLabel idJLabel=new JLabel("身份证号：");
    JTextField idJTe=new JTextField(20);
    JButton sureBtn=new JButton("确定");
    JButton cancelBtn=new JButton("取消");
    JLabel userId=new JLabel("用户编号");
    JLabel userJte=new JLabel();
    public ModifyUserinfor_UI(JFrame jFrame){
        this.mainJFrame=jFrame;
    }
    JOptionPane optionPane=new JOptionPane();
    public void init()  {
        try {
            MyBackGroudJpanel backGroudJpanel=new MyBackGroudJpanel(ImageIO.read(new File("img/regist.jpg")));
            cardJta.setPreferredSize(new Dimension(150,15));
            cardJta.setBorder(BorderFactory.createLineBorder(Color.white,1));
            //cardJta.setText("0123456");
            usernameJTe.setPreferredSize(new Dimension(150,15));
            tellJTe.setPreferredSize(new Dimension(150,15));
            addreddJTe.setPreferredSize(new Dimension(150,15));
            idJTe.setPreferredSize(new Dimension(150,15));
            //用户姓名和消息组装
            Box userBox = Box.createHorizontalBox();
            userBox.add(Box.createHorizontalStrut(100));
            userBox.add(usernameJLabel);
            userBox.add(Box.createHorizontalStrut(20));
            userBox.add(usernameJTe);
            userBox.add(Box.createHorizontalStrut(100));
            //组装电话和地址

            Box tellBox=Box.createHorizontalBox();
            tellBox.add(Box.createHorizontalStrut(100));
            tellBox.add(tellJLabel);
            tellBox.add(Box.createHorizontalStrut(20));
            tellBox.add(tellJTe);
            tellBox.add(Box.createHorizontalStrut(100));

            Box addreddBox=Box.createHorizontalBox();
            addreddBox.add(Box.createHorizontalStrut(100));
            addreddBox.add(addressJLabel);
            addreddBox.add(Box.createHorizontalStrut(20));
            addreddBox.add(addreddJTe);
            addreddBox.add(Box.createHorizontalStrut(100));

            //组装卡号和身份证号
            Box idBox=Box.createHorizontalBox();
            idBox.add(Box.createHorizontalStrut(100));
            idBox.add(cardJLabel);
            idBox.add(Box.createHorizontalStrut(20));
            idBox.add(cardJta);
            idBox.add(Box.createHorizontalStrut(20));
            idBox.add(userId);
            idBox.add(Box.createHorizontalStrut(20));
            idBox.add(userJte);
            idBox.add(Box.createHorizontalStrut(100));


            Box userIdBox=Box.createHorizontalBox();
            userIdBox.add(Box.createHorizontalStrut(100));
            userIdBox.add(idJLabel);
            userIdBox.add(Box.createHorizontalStrut(20));
            userIdBox.add(idJTe);
            userIdBox.add(Box.createHorizontalStrut(100));

            //组装右侧的所有
            Box rightBox=Box.createVerticalBox();
            rightBox.add(Box.createVerticalStrut(60));
            rightBox.add(userBox);
            rightBox.add(Box.createVerticalStrut(20));
            rightBox.add(tellBox);
            rightBox.add(Box.createVerticalStrut(20));
            rightBox.add(addreddBox);
            rightBox.add(Box.createVerticalStrut(20));
            rightBox.add(idBox);
            rightBox.add(Box.createVerticalStrut(20));
            rightBox.add(userIdBox);
            rightBox.add(Box.createVerticalStrut(60));
            JPanel panel=new JPanel();
           // panel.add(cancelBtn);
            panel.add(sureBtn);
            try {
                dialog.setIconImage(ImageIO.read(new File("img/userManage.png")));
            } catch (IOException e) {
                e.printStackTrace();
            }

            sureBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        String tellnum=tellJTe.getText();
                        String address=addreddJTe.getText();
                        String username=usernameJTe.getText();
                        String userid=idJTe.getText();
                        String id=userJte.getText();
//                        System.out.println("tellnum:"+tellnum);
//                        System.out.println("addres:"+address);
//                        System.out.println("username:"+username);
//                        System.out.println("userid:"+userid);
//                        System.out.println("id:"+id);
                        if(tellnum.equals("")||address.equals("")||username.equals("")||userid.equals("")||id.equals("")){
                            JOptionPane.showMessageDialog(mainJFrame,"没有填写完整信息","信息提示",JOptionPane.ERROR);

                            return;
                        }
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project1.1", "root", "jjbone");
                        String sql="update user set user.tellnum =?,user.address=?,user.username=?,user.userid=? where id=? ";
                        PreparedStatement pst = conn.prepareStatement(sql);
                        pst.setString(1,tellnum);
                        pst.setString(2,address);
                        pst.setString(3,username);
                        pst.setString(4,userid);
                        pst.setInt(5,Integer.valueOf(id));
                        int row = pst.executeUpdate();
                        if(row>0){
                            JOptionPane.showMessageDialog(mainJFrame,"修改成功","修改信息提示",JOptionPane.OK_OPTION);
                        }else{
                            JOptionPane.showMessageDialog(mainJFrame,"修改失败","修改信息提示",JOptionPane.OK_OPTION);
                        }
                        pst.close();
                        conn.close();
                    } catch (ClassNotFoundException | SQLException classNotFoundException) {
                        classNotFoundException.printStackTrace();
                    }

                }
            });
//            dialog.addWindowListener(new WindowAdapter() {
//                @Override
//                public void windowClosing(WindowEvent e) {
//
//                }
//            });

            backGroudJpanel.add(rightBox);
            dialog.add(backGroudJpanel);
            dialog.add(panel, BorderLayout.SOUTH);
            dialog.setBounds((ScreenSize.getScreenWidth()-width)/2,(ScreenSize.getScreenHeight()-height)/2,width,height);
            dialog.setResizable(false);
            dialog.setVisible(true);
        } catch (IOException e) {
            e.printStackTrace();
        }






    }

//    public static void main(String[] args) {
//        new ModifyUserinfor_UI(new JFrame()).init();
//    }
}